package mysql

import (
	"bluebell/models"
	"errors"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"strconv"
)

// GetPostTotalCount 获取帖子个数
// 查询失败 ---> tx.Error
// 成功 ---> count,nil
func GetPostTotalCount() (count int64,err error){
	tx := db.Model(&models.Post{}).Count(&count)
	if tx.Error != nil{
		zap.L().Error("dao mysql post GetPostTotalCount() failed",zap.Error(tx.Error))
		return 0,tx.Error
	}
	return count,nil
}

// GetCommunityPostTotalCount 获取communityId下的帖子个数
// 查询失败 ---> 0,tx.Error
// 成功 ---> count,nil
func GetCommunityPostTotalCount(communityId uint64) (count int64, err error){
	tx := db.Model(&models.Post{}).Where("community_id = ?",communityId).Count(&count)
	if tx.Error != nil{
		zap.L().Error("dao mysql post GetCommunityPostTotalCount() failed",zap.Error(tx.Error))
		return 0,tx.Error
	}
	return count,nil
}

// GetPostListTotalCount 根据关键词查询帖子列表总数
// 查询失败 ---> 0,tx.Error
// 成功 ---> count,nil
func GetPostListTotalCount(p *models.ParamPostList)(count int64,err error){
	tx := db.Model(&models.Post{}).Where("title like %?% or content like %?%",p.Search,p.Search).Count(&count)
	if tx.Error != nil{
		zap.L().Error("dao mysql post GetPostListTotalCount() failed",zap.Error(tx.Error))
		return 0,tx.Error
	}
	return count,nil
}
// GetPostList 分页获取帖子列表
// 查询失败 ---> ErrorQueryFailed
// 成功 ---> posts,nil
func GetPostList(page, size int64) (posts []*models.Post,err error){
	total := (page-1)*size
	tx := db.Model(&models.Post{}).Offset(int(total)).Limit(int(size)).Find(&posts)
	err = tx.Error
	if err != gorm.ErrRecordNotFound{
		zap.L().Error("dao mysql post GetPostList() failed",zap.Error(tx.Error))
		return
	}
	return
}

// GetCommunityById 根据CommunityId查询分类社区详情
// 查询失败 ---> tx.Error
// 查询不到数据 ---> ErrorInvalidID
// 成功 ---> community,nil
func GetCommunityById(communityId uint64) (community *models.CommunityDetailRes,err error){
	community = &models.CommunityDetailRes{}
	tx := db.Where("community_id = ?",communityId).Take(&communityId)
	if tx.Error != nil{
		if tx.Error == gorm.ErrRecordNotFound{
			err = errors.New(ErrorInvalidID)
			zap.L().Error("dao mysql post GetCommunityById() failed",zap.Uint64("communityId",communityId),zap.Error(err))
		}else{
			zap.L().Error("dao mysql post GetCommunityById() failed",zap.Error(tx.Error))
		}
		return nil,err
	}
	return community,nil
}

// GetPostById 根据post_id查询帖子详情
// 查询失败 ---> tx.Error
// 查询不到数据 ---> ErrorInvalidID
// 成功 ---> community,nil
func GetPostById(postId int64) (post *models.Post, err error){
	post = &models.Post{}
	tx := db.Model(&models.Post{}).Where("post_id = ?",postId).Take(&post)
	if tx.Error != nil{
		if tx.Error == gorm.ErrRecordNotFound{
			err = errors.New(ErrorInvalidID)
			zap.L().Error("dao mysql post GetPostById() failed",zap.Int64("post_id",postId),zap.Error(err))
		}else{
			zap.L().Error("dao mysql post GetPostById() failed",zap.Error(tx.Error))
		}
		return nil,err
	}
	return post,nil
}

// GetPostListByIds 根据postIds数组 获取帖子列表
func GetPostListByIds(postIds []string)(postList []*models.Post,err error){
	postList = []*models.Post{}
	for _,postId := range postIds{
		post := &models.Post{}
		postid,err := strconv.Atoi(postId)
		if err != nil{
			zap.L().Error("dao mysql post GetPostListByIds() failed",zap.Error(err))
			continue
		}
		tx := db.Where("post_id = ?",postid).Take(&post)
		if tx.Error != nil{
			if tx.Error == gorm.ErrRecordNotFound{
				err := errors.New(ErrorInvalidID)
				zap.L().Error("dao mysql post GetPostListByIds() failed",zap.Int("post_id",postid),zap.Error(err))
				continue
			}else{
				err := errors.New(ErrorQueryFailed)
				zap.L().Error("dao mysql post GetPostListByIds() failed",zap.Error(err))
				continue
			}
		}
		postList = append(postList,post)
	}
	return postList,nil
}

// GetPostListByKeywords  根据keywords去搜索
func GetPostListByKeywords(p *models.ParamPostList)(posts []*models.Post, err error){
	p1 := (p.Page-1)*p.Size
	posts = make([]*models.Post,0,p.Size)
	tx := db.Model(&models.Post{}).Where("title like %?% or content like %?%",p.Search,p.Search).Order("create_time desc").Offset(int(p1)).Limit(int(p.Size)).Find(&posts)
	if tx.Error != nil{
		if tx.Error == gorm.ErrRecordNotFound{
			zap.L().Warn("dao mysql mysql.GetPostListByKeywords() failed",zap.Error(tx.Error))
			return posts,nil
		}
		zap.L().Warn("dao mysql mysql.GetPostListByKeywords() failed",zap.Error(tx.Error))
		return posts,nil
	}
	return posts,nil
}

// CreatePost 创建帖子
func CreatePost(post *models.Post) (err error){
	tx := db.Model(models.Post{}).Create(&post)
	if tx.Error != nil{
		zap.L().Error("insert post failed", zap.Error(err))
		err = errors.New(ErrorInsertFailed)
		return
	}
	return nil
}
